<?php

namespace App\Services\Easemob;
use Easemob\Auth;
use Easemob\Contact;
use Easemob\Group;
use Easemob\User;
use Easemob\UserMetadata;

class EasemobService
{
    protected $auth;

    protected function getAuth()
    {
        if ($this->auth === null) {
            //$this->auth = new Auth("appKey", "Client ID", "ClientSecret");
            //$this->auth = new Auth("1105201105148640#demo", "YXA67Y7RlN_JQvyUr7ql3aPgYA", "YXA6fuTrKOjAu-SLWMTUpOqfYvEBf7s");
            $this->auth = new Auth("1105201105148640#zmkj", "YXA6KplmYKv0SxG-Gy_KpFvL_A", "YXA6WKapMHz9kIqMB0KYlcn19u4FI4U");
        }
        return $this->auth;
    }

    /**
     * 用户注册
     * $userName 用户名
     * $password 密码
     * $nickname 昵称
     */
    public function createUser($userName, $password,$nickname)
    {
        $user = new User($this->getAuth());
        $data = array(
            'username' => $userName,
            'password' =>$password,
            'nickname' =>$nickname,
        );
        return $user->create($data);
    }

    /**
     * 获取单个用户的详细信息
     * $username  用户名
     */
    public function get($username){
        $user = new User($this->getAuth());
        return $user->get($username);
    }


     /**
     * 添加好友
     * $username  要添加好友的用户名
     * $contact   好友用户名
     */
    public function add($username, $contact){
        $usercontact = new Contact($this->getAuth());
        return $usercontact->add($username, $contact);
    }

     /**
     * 好友列表
     * $username  要获取好友列表的用户名
     *  
     */
    public function get_friendlist($username){
        $usercontact = new Contact($this->getAuth());
        return $usercontact->get($username);
    }

    /**
     * 获取单个用户加入的所有群组
     */
    public function get_user_groupall($username){
        $userGroup = new Group($this->getAuth());
        return $userGroup->listAllGroupsUserJoined($username);
    }
    

     //获取环信token
     public function hxtoken(){
        $Auth = new Auth("1105201105148640#zmkj", "YXA6KplmYKv0SxG-Gy_KpFvL_A", "YXA6WKapMHz9kIqMB0KYlcn19u4FI4U");
        return $Auth->getEasemobToken();
     }
     //获取用户token
     public function usertoken($username){
        $Auth = new Auth("1105201105148640#zmkj", "YXA6KplmYKv0SxG-Gy_KpFvL_A", "YXA6WKapMHz9kIqMB0KYlcn19u4FI4U");
        $password = "123456";
        return $Auth->getUserToken($username,$password);
     }
 
    
    /**
     * 设置用户属性
     * $username  要设置属性的用户名
     * $contact   要设置的属性（键：属性名；值：属性值）
     */
    public function attribute($username, $metadata){
       
        $UserMetadata = new UserMetadata($this->getAuth());
        return $UserMetadata->setMetadataToUser($username, $metadata);
    }

    /**
     * 获取用户属性
     * $username  要设置属性的用户名
     * $contact   要设置的属性（键：属性名；值：属性值）
     */
    public function getMetadataFromUser($username){
        $UserMetadata = new UserMetadata($this->getAuth());
        return $UserMetadata->getMetadataFromUser($username);
    }


}